//*************************************************
//Program #4       main.cpp
//*************************************************
//Names: Stephen Durfrey,  Andrew Brock
//Due: April 23, 2010
//Operating Systems
//
//About: This assignment is to help better-understand page
//  replacement algorithms (PRA) in virtual memory. Our task is
//  to simulate some of the common page replacement 
//  algorithms discussed in class and compare performances.
//Files: main.cpp, FIFO.h, FIFO.cpp, Optimal.h, Optimal.cpp
//  Page_Replacement_Algorith.h, Page_Replacement_Algorith.cpp
//  LFU.h, LFU.cpp, LRU.h, LRU.cpp, MFU.h, MFU.cpp, SCA.h,
//  and SCA.cpp.
//Description: These are the PRAs covered in our program. 
// FIFO PRA: Simplest pra, associates with each page the time
//  when that page was brought to memory.
// Optimal PRA: Guaruntees the lowest possible page fault rate
//  for a fixed number of frames.
// Least-Recently-Used PGA: Associates with each page the time
//  of that pages' last use.
// Second-Chance PGA: FIFO-like algorithm with a second chance
//  to "survive" in memory.
// Least-Frequently-Used PGA: the page with the smallest count 
//  be replaced.
// Most-Frequently-Used PGA: The page with the largest count 
//  be replaced.
//Input:  ref_string_1.txt or ref_string_2.txt, and 
//  number of frames.
//Execute: Example "a.out ref_string_1.txt 2"
//Output: FIFO.txt, LFU.txt, LRU.txt, MFU.txt, Optimal.txt,
//    and SCA.txt

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "FIFO.h"
#include "Optimal.h"
#include "LRU.h"
#include "SCA.h"
#include "LFU.h"
#include "MFU.h"

using namespace std;

void createRefString(vector<int> &, string file_name);

int main(int argc, char** argv)
{
	//Declarations:
	FIFO pra;
	Optimal opt;
	LRU leastRU;
	SCA scAlgo;
	LFU lfuAlgo;
	MFU mfuAlgo;
	vector<int>::iterator begin, end;
	
	
	string file_name = argv[1];
	int num_frames = atoi(argv[2]);
	vector<int> ref_string;
	list<int> ref_string_list;

	//Initialize Statements
	createRefString(ref_string, file_name);
	pra.init(num_frames, ref_string);
	opt.init(num_frames, ref_string);
	scAlgo.init(num_frames, ref_string);
	lfuAlgo.init(num_frames, ref_string);
	mfuAlgo.init(num_frames, ref_string);

	//Puts the reference string into a list, for the LRU algorithm
	for(begin = ref_string.begin(); begin != ref_string.end(); begin++)
	{
		ref_string_list.push_back(*begin);
	}

	leastRU.init(num_frames, ref_string_list);

	//Execute The Different Algorithms
	pra.run();
	opt.run();
	leastRU.run();
	scAlgo.run();
	lfuAlgo.run();
	mfuAlgo.run();

	return 0;
}

void createRefString(vector<int> &ref, string file_name)
{
	ifstream file;
	int temp;
	

	file.open(file_name.c_str());

	if(file.is_open())
	{
		while(!file.eof())
		{
			file >> temp;
			ref.push_back(temp);
		}
	}
	else
	{
		cout << "file opening failed\n";
		exit(1);
	}
}

